package com.manong.bookstore.shoppingcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.manong.bookstore.shoppingcart.vo.SCartProduceInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.Query;

import com.manong.bookstore.shoppingcart.dao.ShoppingCartDao;
import com.manong.bookstore.shoppingcart.entity.ShoppingCartEntity;
import com.manong.bookstore.shoppingcart.service.ShoppingCartService;


@Service("shoppingCartService")
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCartEntity> implements ShoppingCartService {

    @Autowired
    ShoppingCartDao shoppingCartDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShoppingCartEntity> page = this.page(
                new Query<ShoppingCartEntity>().getPage(params),
                new QueryWrapper<ShoppingCartEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据session中的userID来查询用户的购物车
     * @return
     */
    @Override
    public List<SCartProduceInfo> ListUserShoppingCart(Integer userID) {
        //String userID=
        Map<String,Object> map=new HashMap<>();
        map.put("user_id",userID);
        List<ShoppingCartEntity> shoppingCartEntities = shoppingCartDao.selectByMap(map);
        List<SCartProduceInfo> collect = shoppingCartEntities.stream().map(item -> {
            SCartProduceInfo sCartProduceInfo = new SCartProduceInfo();
            BeanUtils.copyProperties(item, sCartProduceInfo);
            return sCartProduceInfo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public void removeByProductId(Integer usrId, Integer[] id) {
        Map<String,Object> map=new HashMap<>();
        map.put("user_id",usrId);
        for (int i=0;i<id.length;i++) {
            map.put("produce_id", id[i]);
            shoppingCartDao.deleteByMap(map);
        }
    }

    @Override
    public void updateByProduceId(ShoppingCartEntity shoppingCart, Integer userId) {
        shoppingCartDao.update(shoppingCart,new UpdateWrapper<ShoppingCartEntity>()
                .eq("produce_id",shoppingCart.getProduceId())
                .eq("user_id",userId));
    }

    @Override
    public List<ShoppingCartEntity> listSelectProduce(Integer userId, Integer[] select) {
        List<ShoppingCartEntity> shoppingCartEntities = baseMapper.selectList(new QueryWrapper<ShoppingCartEntity>().eq("user_id",userId).in("produce_id",select));
        return shoppingCartEntities;
    }

    @Override
    public ShoppingCartEntity ProduceExistOrNot(Integer userId, Integer produceId) {
        ShoppingCartEntity shoppingCartEntity = baseMapper.selectOne(new QueryWrapper<ShoppingCartEntity>().eq("user_id", userId).eq("produce_id", produceId));
        return shoppingCartEntity;
    }

}